package com.wiscofile.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wiscofile.pojo.WiscoDataBianzhi;

/**
 * A data access object (DAO) providing persistence and search support for
 * WiscoDataBianzhi entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.wiscofile.pojo.WiscoDataBianzhi
 * @author MyEclipse Persistence Tools
 */

public class WiscoDataBianzhiDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(WiscoDataBianzhiDAO.class);

	public boolean save(WiscoDataBianzhi transientInstance) {
		try {
			Session session = getSession();
			Transaction tra = session.beginTransaction();
			session.save(transientInstance);
			tra.commit();
			session.close();
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			re.printStackTrace();
			return false;
		}
	}
	
	public boolean update(WiscoDataBianzhi transientInstance) {
		try {
			Session session = getSession();
			Transaction tra = session.beginTransaction();
			session.clear();
			session.update(transientInstance);
			tra.commit();
			session.close();
			return true;
		} catch (RuntimeException re) {
			log.error("update failed", re);
			re.printStackTrace();
			return false;
		}
	}

	public boolean delete(WiscoDataBianzhi persistentInstance) {
		try {
			Session session = getSession();
			Transaction tra = session.beginTransaction();
			session.delete(persistentInstance);
			tra.commit();
			session.close();
			return true;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			re.printStackTrace();
			return false;
		}
	}

	public WiscoDataBianzhi findById(java.lang.String id) {
		try {
			Session session = getSession();
			WiscoDataBianzhi instance = (WiscoDataBianzhi) session.get(
					"com.wiscofile.pojo.WiscoDataBianzhi", id);
			session.close();
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	
	public List findAll(int first,int pagesize) {
		try {
			Session session = getSession();
			String queryString = "from WiscoDataBianzhi";
			Query queryObject = session.createQuery(queryString);
			queryObject.setFirstResult(first);
			queryObject.setMaxResults(pagesize);
			List list = queryObject.list();
			session.close();
			return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	public int countAll() {
		try {
			Session session = getSession();
			String queryString = " select count(w) from WiscoDataBianzhi w";
			Query queryObject = session.createQuery(queryString);
			Long count = (Long) queryObject.list().get(0);
			session.close();
			return count.intValue();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public WiscoDataBianzhi merge(WiscoDataBianzhi detachedInstance) {
		log.debug("merging WiscoDataBianzhi instance");
		try {
			WiscoDataBianzhi result = (WiscoDataBianzhi) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(WiscoDataBianzhi instance) {
		log.debug("attaching dirty WiscoDataBianzhi instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(WiscoDataBianzhi instance) {
		log.debug("attaching clean WiscoDataBianzhi instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	public static void main(String[] args) {
		WiscoDataBianzhiDAO wdbDAO = new WiscoDataBianzhiDAO();
		Session session = wdbDAO.getSession();
		Transaction ts =  session.beginTransaction();
		WiscoDataBianzhi wiscoDataBianzhi = new WiscoDataBianzhi();
		wiscoDataBianzhi.setDbid("2");
		wiscoDataBianzhi.setCheckdate(new Date());
		session.save(wiscoDataBianzhi);
		ts.commit();
	}
}